package gat.api.rest.background.backgroundUser;

import java.util.List;

import javax.validation.Valid;

import gat.api.config.annotation.CheckPermissions;
import gat.api.config.annotation.Log;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.bitsun.core.common.web.ResultDTO;

import gat.api.background.backgroundUser.BPermissionsApi;
import gat.application.backgroundUser.PermissionsService;
import gat.dto.req.backgroundUser.PermissionsReqDto;
import gat.dto.req.backgroundUser.UserRoleReqDto;
import gat.dto.res.backgroundUser.PermissionsResDto;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;

/**
 * 权限表
 * @Author: tmac
 * @Email: tmac
 */
@Slf4j
@SuppressWarnings("unchecked")
@RestController
@RequestMapping("/permissions")
public class BPermissionsApiController implements BPermissionsApi {


    @Autowired
    private PermissionsService permissionsService;


    @Override
    @PostMapping(value = "/getByUserCode" ,consumes = {"application/json"}, produces = {"application/json"})
    public ResultDTO<List<PermissionsResDto>> getPermissionsByUserId(@Valid  @RequestBody UserRoleReqDto reqDto) {
        List<PermissionsResDto> resDtos =permissionsService.getPermissionsByUserId(reqDto);
        return ResultDTO.ok(resDtos);
    }

    @Override
    @Log(value = "菜单列表查询")
    @CheckPermissions(value = "permissions:list")
    @GetMapping(value = "/getPermissionsList" ,consumes = {"application/json"}, produces = {"application/json"})
    public ResultDTO<List<PermissionsResDto>> getPermissionsList(@Valid  @RequestBody UserRoleReqDto reqDto) {
        List<PermissionsResDto> resDtos =permissionsService.getPermissionsList(reqDto);
        return ResultDTO.ok(resDtos);
    }


    @Override
    @Log(value = "菜单列表保存")
    @CheckPermissions(value = "permissions:save")
    @PostMapping(consumes = {"application/json"}, produces = {"application/json"})
    public ResultDTO<PermissionsResDto> addPermissions(
        @ApiParam(value = "要保存的对象", required = true) @Valid @RequestBody PermissionsReqDto reqDto) {
        PermissionsResDto resDto = permissionsService.save(reqDto);
        return ResultDTO.ok(resDto);
    }

    @Override
    @Log(value = "菜单列表删除")
    @CheckPermissions(value = "permissions:delete")
    @DeleteMapping(value = "/{ids}", produces = {"application/json"})
    public ResultDTO<Void> deletePermissions(
        @ApiParam(value = "要删除的对象主键，多个对象主键可以用英文逗号隔开", required = true) @PathVariable("ids") String ids) {
        boolean bl = permissionsService.deleteByIds(ids);
        return ResultDTO.ok();
    }

    @Override
    @GetMapping(value = "/{id}", produces = {"application/json"})
    public ResultDTO<PermissionsResDto> getOnePermissions(@ApiParam(value = "要查询的对象主键", required = true) @PathVariable("id") Long id) {
        PermissionsResDto resDto = permissionsService.selectOne(id);
        return ResultDTO.ok(resDto);
    }

    @Override
    @Log(value = "菜单列表编辑")
    @CheckPermissions(value = "permissions:edit")
    @PatchMapping(value = "/{id}", produces = {"application/json"})
    public ResultDTO<PermissionsResDto> patchUpdatePermissions(@ApiParam(value = "对象ID", required = true) @PathVariable("id") Long id,
                                                               @ApiParam(value = "要修改的对象，对象属性有值的才更新,即null值不更新", required = true) @Valid @RequestBody PermissionsReqDto reqDto) {
        permissionsService.updateProps(id, reqDto);
        PermissionsResDto resDto = permissionsService.selectOne(id);
        return ResultDTO.ok(resDto);
    }

    @Override
    @GetMapping(value = "/get/allPermissionsList")
    public ResultDTO<List<PermissionsResDto>> getAllPermissionsList() {
        List<PermissionsResDto> resDtos =permissionsService.getAllPermissionsList();
        return ResultDTO.ok(resDtos);
    }

    @Override
    @GetMapping(value = "/get/indexPermissionsList")
    public ResultDTO<List<PermissionsResDto>> indexPermissionsList() {
        List<PermissionsResDto> resDtos =permissionsService.getIndexPermissionsList();
        return ResultDTO.ok(resDtos);
    }

}
